package git.jasonch.isp.utils;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/**
 * projectName : LSP
 * className: ScriptForISP
 * description: 为了学校的isp实现自动打卡，写个定时脚本帮助用户实现其功能。
 *
 * @author : git.jas0nch
 * date: 2022-02-05
 */
public class ScriptForISPUtils {
    @Test
    public void func() throws InterruptedException{
        try{
            scriptForISP();
        }catch (UnhandledAlertException e){
            System.out.println("您已经登录过了哦~~~");
        }
    }

    // the script method
    public void scriptForISP() throws InterruptedException{
        WebDriver webDriver = null;
        try {
            // windows, D:\JavaProjectLearning\LSP\src\main\resources\
//        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
            ChromeOptions chromeOptions=new ChromeOptions();
//            chromeOptions.setHeadless(Boolean.TRUE);
            chromeOptions.addArguments("--no-sandbox","--disable-dev-shm-usage","window-size=1920x3000","--disable-gpu","--hide-scrollbars","blink-settings=imagesEnabled=false","--headless");
            //启动一个 chrome 实例
            // linux
            System.setProperty("webdriver.chrome.driver", "/usr/lib64/chromium-browser/chromedriver");

            webDriver = new ChromeDriver(chromeOptions);

            /*
             * @description : 打开isp地址，进行登录操作
             * @param
             * @return : void
             * @author : git.jas0nch
             * date : 2022/2/5
             **/
            webDriver.get("https://xsswzx.cdu.edu.cn/ispstu1-2/com_user/weblogin.asp");
            // get the validCode:
            String validCode = webDriver.findElement(By.xpath("//*[@id=\"form\"]/div[3]")).getText().substring(2);
            // login here:
            webDriver.findElement(By.id("username")).sendKeys("201910414303");
            Thread.sleep(1000);
            webDriver.findElement(By.id("userpwd")).sendKeys("037315");
            Thread.sleep(1000);
            webDriver.findElement(By.id("code")).sendKeys(validCode);
            Thread.sleep(1000);
            // submit:
            webDriver.findElement(By.id("提交")).click();
            Thread.sleep(1000);
            // after login, to find the isp register, and to submit the information;
            webDriver.get("https://xsswzx.cdu.edu.cn/ispstu1-2/com_user/project.asp?id=d3c87b7b13b404fe5c12cad9a638208286065a4ebc69");
            Thread.sleep(1000);
            // //*[@id="t"]/tbody/tr[6]/td/a[1]: then 点击常规登记（因为每隔5天要重置信息，所以直接常规登记。。。暂时）
            webDriver.findElement(By.xpath("//*[@id=\"t\"]/tbody/tr[6]/td/a[1]")).click();
            Thread.sleep(1000);
            webDriver.manage().window().maximize();
            // select tag: 这里不用select选项和option进行，比较麻烦。直接input；
            // 如果用 select，可参考：https://blog.csdn.net/lmarster/article/details/86138926
            webDriver.findElement(By.xpath("//*[@id=\"province\"]")).sendKeys("四川省");
            Thread.sleep(500);
            webDriver.findElement(By.xpath("//*[@id=\"city\"]")).sendKeys("泸州市");
            Thread.sleep(500);
            webDriver.findElement(By.xpath("//*[@id=\"area\"]")).sendKeys("合江县");
            Thread.sleep(500);
            // submit：
            webDriver.findElement(By.xpath("/html/body/form/table/tbody/tr[13]/td/div/input[1]")).click();
            webDriver.manage().window().maximize();
            Thread.sleep(2000);
            // close
            webDriver.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            if (webDriver != null) {
                //退出 chrome
                webDriver.quit();
            }
        }
    }
}
